package com.qianbei.user.applymaster;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.ext.HttpCallback;
import com.android.volley.ext.RequestInfo;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.base.BaseApplication;
import com.qianbei.common.log.MLog;
import com.qianbei.common.net.control.Constant;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerControl.ServerListener;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.net.view.ProgressDialog;
import com.qianbei.common.utilis.img.CanmlTools;
import com.qianbei.common.utilis.img.CanmlTools.CamerCallback;
import com.qianbei.common.utilis.img.ImageTools;
import com.qianbei.user.applymaster.ShowImage.ImageClick;
import com.qianbei.user.model.UserMode;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 申请成为前辈 资料填写页面
 * 
 * @author gml
 * 
 */
public class ApplyRegistActivity extends BaseActivity implements CamerCallback,
		ImageClick {

	private EditText name, country, et_city2, et_introduce, email, touxie;

	private ImageView impro_nan, impro_nv, perceter_icon;

	private String sexString = "1";// 1男 0女

	private CanmlTools canmlTools = null;

	private ShowImage[] upArry = new ShowImage[4];
	private ArrayList<String> fileArry = new ArrayList<String>();

	private ArrayList<String> idArry = new ArrayList<String>();

	private RequestQueue mQueue;

	private ProgressDialog upDialog;// new
									// ProgressDialog(mContext).showProgress("正在上传");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mQueue = Volley.newRequestQueue(BaseApplication.mApplication);
		setContentView(R.layout.apply_regist_activity);
		name = (EditText) findViewById(R.id.et_name);
		et_city2 = (EditText) findViewById(R.id.et_city2);
		country = (EditText) findViewById(R.id.country);
		et_introduce = (EditText) findViewById(R.id.et_introduce);
		impro_nan = (ImageView) findViewById(R.id.impro_nan);
		impro_nv = (ImageView) findViewById(R.id.impro_nv);

		email = (EditText) findViewById(R.id.et_email);
		touxie = (EditText) findViewById(R.id.et_touxie);

		perceter_icon = (ImageView) findViewById(R.id.perceter_icon);
		impro_nv.setOnClickListener(this);
		impro_nan.setOnClickListener(this);
		setTitle("申请成为前辈");
		findViewById(R.id.btn_login).setOnClickListener(this);
		Register();
		canmlTools = new CanmlTools(mContext, this);

		upArry[0] = (ShowImage) findViewById(R.id.upimage1);
		upArry[0].ids = 0;
		upArry[1] = (ShowImage) findViewById(R.id.upimage2);
		upArry[1].ids = 1;
		upArry[2] = (ShowImage) findViewById(R.id.upimage3);
		upArry[2].ids = 2;
		upArry[3] = (ShowImage) findViewById(R.id.upimage4);
		upArry[3].ids = 3;

		upArry[0].setOnClickListener(this);
		upArry[1].setOnClickListener(this);
		upArry[2].setOnClickListener(this);
		upArry[3].setOnClickListener(this);

		upArry[0].click = this;
		upArry[1].click = this;
		upArry[2].click = this;
		upArry[3].click = this;

		upDialog = new ProgressDialog(mContext);// .showProgress("正在上传");

	}

	public boolean emailValidation(String email) {
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		return email.matches(regex);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.btn_login:

			hideSoftInputFromWindow();
			// Util.hideSoftInputFromWindow(v);
			//
			if (TextUtils.isEmpty(name.getText().toString().trim())) {
				new MsgAlert().show("名称不能为空");
				return;
			}
			if (TextUtils.isEmpty(country.getText().toString().trim())) {
				new MsgAlert().show("国家不能为空");
				return;
			}
			if (TextUtils.isEmpty(et_city2.getText().toString().trim())) {
				new MsgAlert().show("城市不能为空");
				return;
			}
			if (TextUtils.isEmpty(touxie.getText().toString().trim())) {
				new MsgAlert().show("职位名称不能为空");
				return;
			}
			if (TextUtils.isEmpty(et_introduce.getText().toString().trim())) {
				new MsgAlert().show("个人介绍不能为空");
				return;
			}
			if (TextUtils.isEmpty(email.getText().toString().trim())) {
				new MsgAlert().show("邮箱不能为空");
				return;
			}
			if (!emailValidation(email.getText().toString().trim())) {
				new MsgAlert().show("邮箱格式不对");
				return;
			}

			UserMode bean = new UserMode();
			bean.name = name.getText().toString();
			bean.sex = sexString;
			bean.career = et_introduce.getText().toString();
			bean.identity = touxie.getText().toString();
			bean.city = et_city2.getText().toString();
			bean.emaiString = email.getText().toString();
			bean.country = country.getText().toString();

			for (ShowImage beans : upArry) {

				if (!TextUtils.isEmpty(beans.id)) {
					bean.imgidArrayList.add(beans.id);
				}

			}



			// improvmentData("", name.getText().toString(), sexString,
			// et_introduce.getText().toString(), country.getText()
			// .toString(), et_city2.getText().toString(), "");
			// }
			break;
		case R.id.impro_nan:
			sexString = "1";
			impro_nan.setBackgroundResource(R.drawable.user_improvment_sex_1);
			impro_nv.setBackgroundResource(R.drawable.user_improvment_sex_2);
			break;
		case R.id.impro_nv:
			sexString = "0";
			impro_nan.setBackgroundResource(R.drawable.user_improvment_sex_2);
			impro_nv.setBackgroundResource(R.drawable.user_improvment_sex_1);
			break;

		case R.id.upimage1:
		case R.id.upimage2:
		case R.id.upimage3:
		case R.id.upimage4:
			canmlTools.showCanmel(v.getId(), 2);
			break;
		default:
			break;
		}
	}

	/**
	 * 获取用户资料
	 */
	private void Register() {
		ServerControl sc = new ServerControl(Method.GET, URL
				+ "transaction_app/users/user_info?");
		sc.serverListener = new ServerListener() {

			@Override
			public void serverFinish(ServerResult sr) {

				if (sr.isContinue) {
					UserMode bean = JSON.parseObject(
							sr.bodyData.optJSONObject("userInfo").toString(),
							UserMode.class);
					if(!((BaseActivity) mContext).isFinishing()) {
						Glide.with(mContext)
								.load(Constant.URL + bean.profile_image_url)
								.placeholder(R.drawable.ic_launcher)
								.into(perceter_icon);
					}
					name.setText(bean.name);

					if (bean.sex.equals("true")) {
						sexString = "true";
						impro_nan
								.setBackgroundResource(R.drawable.user_improvment_sex_1);
						impro_nv.setBackgroundResource(R.drawable.user_improvment_sex_2);
					} else {
						sexString = "false";
						impro_nan
								.setBackgroundResource(R.drawable.user_improvment_sex_2);
						impro_nv.setBackgroundResource(R.drawable.user_improvment_sex_1);
					}
					et_introduce.setText(bean.career);
				}
			}
		};
		new ProgressDialog(mContext).startControl(sc);

	}

	@Override
	public void camerPickphoto(String imageUri, int i) {
		MLog.dLog("imageUri:" + imageUri);
		Bitmap tempbBitmap = ImageTools.bitmapFromPath(imageUri,
				(int) ImageTools.getDensity() * 65,
				(int) ImageTools.getDensity() * 65);

		switch (i) {
		case R.id.upimage1:
			fileArry.add(imageUri);
			upArry[0].file = imageUri;
			upArry[0].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), tempbBitmap));
			upLoadImage(imageUri, new HttpCall(0));
			break;

		case R.id.upimage2:
			// imgData = imageUri;
			fileArry.add(imageUri);
			upArry[1].file = imageUri;
			upArry[1].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), tempbBitmap));
			upLoadImage(imageUri, new HttpCall(1));
			break;
		//
		case R.id.upimage3:
			fileArry.add(imageUri);
			upArry[2].file = imageUri;
			// imgData = imageUri;
			upArry[2].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), tempbBitmap));
			upLoadImage(imageUri, new HttpCall(2));
			break;
		//
		case R.id.upimage4:
			fileArry.add(imageUri);
			upArry[3].file = imageUri;
			// imgData = imageUri;
			upArry[3].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), tempbBitmap));
			upLoadImage(imageUri, new HttpCall(3));
			break;

		default:
			break;
		}

	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {
			return;
		}
		canmlTools.onresult(requestCode, data);
	}

	/** 上传图片 */
	private void upLoadImage(String imgData, HttpCallback back) {
		ServerControl sr = new ServerControl();

		Map<String, Object> params = new HashMap<String, Object>();

		if (imgData != null) {
			params.put("file", new File(imgData));
		}
		RequestInfo requestInfo = new RequestInfo();
		requestInfo.url = upImage;
		requestInfo.putAllParams(params);
		sr.upload(requestInfo, back);
		upDialog.showProgress("正在上传");
	}

	class HttpCall implements HttpCallback {

		private int id;

		public HttpCall(int id) {
			this.id = id;
		}

		@Override
		public void onStart() {

		}

		@Override
		public void onResult(String string) {

			try {
				JSONObject jsonObject = new JSONObject(string);
				if (jsonObject.optBoolean("status")) {

					if (!TextUtils.isEmpty(jsonObject.optJSONObject("picture")
							.optString("id"))) {
						upArry[id].id = jsonObject.optJSONObject("picture")
								.optString("id");
					}

					switch (id) {
					case 0:
						upArry[0].delet_imge.setVisibility(View.VISIBLE);
						upArry[1].setVisibility(View.VISIBLE);
						break;
					case 1:
						upArry[1].delet_imge.setVisibility(View.VISIBLE);
						upArry[2].setVisibility(View.VISIBLE);
						break;
					case 2:
						upArry[2].delet_imge.setVisibility(View.VISIBLE);
						upArry[3].setVisibility(View.VISIBLE);
						break;
					case 3:
						upArry[3].delet_imge.setVisibility(View.VISIBLE);
						// upArry[1].setVisibility(View.VISIBLE);
						break;

					default:
						break;
					}
					// SetData.setFace(jsonObject.optString("face"));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onLoading(long count, long current) {

		}

		@Override
		public void onFinish() {
			upDialog.close();
		}

		@Override
		public void onError(Exception e) {

		}

		@Override
		public void onCancelled() {

		}

	}

	@Override
	public void deletImage(int ids) {

		deletID(upArry[ids].id + "", ids);

	}

	private void deletID(String id, final int ids) {
		ServerControl srControl = new ServerControl(Method.DELETE, Constant.URL
				+ "transaction_app/pictures/" + id);
		srControl.serverListener = new ServerListener() {
			@Override
			public void serverFinish(ServerResult sr) {
				if (sr.isContinue) {

					fileArry.remove(upArry[ids].file);
					upArry[ids].id = "";
					refreshView();
					// ShowImage.this.setVisibility(View.GONE);
				}
			}
		};
		// srControl.startVolley();
		new ProgressDialog(mContext).startControl(srControl, "正在删除");
	}

	private void refreshView() {
		int size = fileArry.size();

		upArry[0].setVisibility(View.GONE);
		upArry[0].show_imge.setImageResource(R.drawable.icon_upload_default);
		upArry[1].setVisibility(View.GONE);
		upArry[1].show_imge.setImageResource(R.drawable.icon_upload_default);
		upArry[2].setVisibility(View.GONE);
		upArry[2].show_imge.setImageResource(R.drawable.icon_upload_default);
		upArry[3].setVisibility(View.GONE);
		upArry[3].show_imge.setImageResource(R.drawable.icon_upload_default);
		switch (size) {
		case 0:
			upArry[0].setVisibility(View.VISIBLE);
			upArry[0].delet_imge.setVisibility(View.GONE);
			upArry[0].show_imge
					.setImageResource(R.drawable.icon_upload_default);
			break;

		case 1:

			upArry[0].setVisibility(View.VISIBLE);
			upArry[0].delet_imge.setVisibility(View.VISIBLE);

			upArry[0].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), ImageTools.bitmapFromPath(fileArry.get(0),
							(int) ImageTools.getDensity() * 65,
							(int) ImageTools.getDensity() * 65)));

			upArry[1].setVisibility(View.VISIBLE);
			upArry[1].delet_imge.setVisibility(View.GONE);
			// upArry[1].show_imge.setImageResource(R.drawable.icon_upload_default);

			break;
		case 2:

			upArry[0].setVisibility(View.VISIBLE);
			upArry[0].delet_imge.setVisibility(View.VISIBLE);
			upArry[0].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), ImageTools.bitmapFromPath(fileArry.get(0),
							(int) ImageTools.getDensity() * 65,
							(int) ImageTools.getDensity() * 65)));

			upArry[1].setVisibility(View.VISIBLE);
			upArry[1].delet_imge.setVisibility(View.VISIBLE);
			upArry[1].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), ImageTools.bitmapFromPath(fileArry.get(1),
							(int) ImageTools.getDensity() * 65,
							(int) ImageTools.getDensity() * 65)));

			upArry[2].setVisibility(View.VISIBLE);
			upArry[2].delet_imge.setVisibility(View.GONE);
			// upArry[2].show_imge.setImageResource(R.drawable.icon_upload_default);

			break;
		case 3:

			upArry[0].setVisibility(View.VISIBLE);
			upArry[0].delet_imge.setVisibility(View.VISIBLE);
			upArry[0].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), ImageTools.bitmapFromPath(fileArry.get(0),
							(int) ImageTools.getDensity() * 65,
							(int) ImageTools.getDensity() * 65)));

			upArry[1].setVisibility(View.VISIBLE);
			upArry[1].delet_imge.setVisibility(View.VISIBLE);
			upArry[1].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), ImageTools.bitmapFromPath(fileArry.get(1),
							(int) ImageTools.getDensity() * 65,
							(int) ImageTools.getDensity() * 65)));

			upArry[2].setVisibility(View.VISIBLE);
			upArry[2].delet_imge.setVisibility(View.VISIBLE);
			upArry[2].show_imge.setImageDrawable(new BitmapDrawable(
					getResources(), ImageTools.bitmapFromPath(fileArry.get(2),
							(int) ImageTools.getDensity() * 65,
							(int) ImageTools.getDensity() * 65)));

			upArry[3].setVisibility(View.VISIBLE);
			upArry[3].delet_imge.setVisibility(View.GONE);
			// upArry[3].show_imge.setImageResource(R.drawable.icon_upload_default);

			break;

		default:
			break;
		}
	}
}
